package fnord.jovian.tango;

import java.awt.Point;

import fnord.jovian.TheBoard;
import fnord.jovian.tango.TheAssemblyLayout.TheAssemblyOrientation;

public class TheChatPanel extends ThePanel {

	private TheTextArea _area;
	private TheTextField _field;

	public TheChatPanel(TheBoard board) {
		super(0,0,200,200,board);
		TheAssemblyLayout layout = new TheAssemblyLayout(TheAssemblyOrientation.VERTICAL,10);
		TheAssemblyLayout horzLayout = new TheAssemblyLayout(TheAssemblyOrientation.HORIZONTAL, 0);
		setTheLayout(layout);
		_area = new TheTextArea(180,120,board);
		addComponent(_area);
		_field = new TheTextField(board,180,25);
		_field.setActive(true);
		addComponent(_field);
		ThePanel buttonPanel = new ThePanel(0,0,180,30,board);
		buttonPanel.setTheLayout(horzLayout);
		buttonPanel.addComponent(new TheButton("Send Chat", 80, 30, new TheSendListener(), 10));
		buttonPanel.addComponent(new TheButton("Clear Log", 80, 30, new TheClearListener(), 10));
		buttonPanel.showBackground(false);
		addComponent(buttonPanel);
	}

	public void addText(String t) {
		_area.addText(t);
	}

	private class TheSendListener implements TheListener {
		public void doAction() {
			sendChat();
		}
	}

	public void sendChat() {
		_board.sendChat(_field.getValue());
		_field.setValue("");
	}
	
	private class TheClearListener implements TheListener {
		public void doAction() {
			_area.clearText();
		}
	}

	public void activatePanel(boolean active) {
		_field.setEditable(active);
		_field.setGray(active);
		_area.setActive(active);
	}

	public boolean contains(Point p) {
		return (p.getX() > this.getX() && p.getX() < this.getX() + this.getWidth()
				&& p.getY() > this.getY() && p.getY() < this.getY() + this.getHeight());
	}
	
	public void grabFocus() {
		_field.grabFocus();
	}
	
	public boolean isFocused() {
		return _field.isFocused();
	}
}
